package reduce.fz.com.reduce.activity;

import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.custom.TabIndicator;
import reduce.fz.com.reduce.util.ReduceLog;
import reduce.fz.com.reduce.util.TipUtil;
import reduce.fz.com.reduce.util.base.CrashHandler;
import reduce.fz.com.reduce.util.base.ReduceApplication;

public abstract class BaseToolBarActivity extends ActionBarActivity {
    public FrameLayout mContainerFrameLayout;
    public Toolbar mToolbar;
    public TextView mTitleView;
    protected ImageView barLeftBtn;
    protected TextView mRightBtn;
    private int tabmargin_x;
    //中间切换选项
    protected TabIndicator mBarTab;

    protected void displayLeftBtn() {
        barLeftBtn.setVisibility(View.VISIBLE);
    }
    public void setTabBar(List<String> titles, TabIndicator.OnTabSelectedListener tabSelectListener) {
        mBarTab.setTabMarginX(tabmargin_x, tabmargin_x);
        mBarTab.setVisibility(View.VISIBLE);
        mTitleView.setVisibility(View.GONE);
        if (titles != null)
            mBarTab.setTitleList(titles);
        if (tabSelectListener != null)
            mBarTab.setOnTabSelectedListener(tabSelectListener);
    }

    public void setTabBarPositon(int positon) {
        mBarTab.setCurrentItem(positon);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_tool_bar);
        mContainerFrameLayout = (FrameLayout) findViewById(R.id.container);
        tabmargin_x = getResources().getDimensionPixelSize(R.dimen.dimen_60);
        // 设置窗口风格为顶部显示Actionbar
        mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
        mBarTab = (TabIndicator) findViewById(R.id.bar_tab);
        mTitleView = (TextView) mToolbar.findViewById(R.id.toolbar_title);
        barLeftBtn = (ImageView) mToolbar.findViewById(R.id.bar_left_btn);
        mRightBtn = (TextView) mToolbar.findViewById(R.id.toobar_right_btn);
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayShowTitleEnabled(false);
        displayLeftBtn();
        barLeftBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    onBackPressed();
                } catch (Throwable e) {
                    ReduceLog.e(e);
                }
            }
        });
        ReduceApplication.getInstance().addActivity(this);
        //统一异常管理
        //   CrashHandler crashHandler = CrashHandler.getInstance();
        // crashHandler.init(this);
    }

    public void setToolbarTitle(String title) {
        mTitleView.setVisibility(View.VISIBLE);
        mBarTab.setVisibility(View.GONE);
        mTitleView.setText(title);
    }

    /**
     * 设置标题按钮
     *
     * @param title
     * @return
     */
    public TextView setRightBtn(String title) {
        mRightBtn.setBackgroundResource(R.drawable.toolbar_btn_selector);
        int left = getResources().getDimensionPixelSize(R.dimen.dimen_9);
        int top = getResources().getDimensionPixelSize(R.dimen.dimen_18);
        mRightBtn.setPadding(left, top, left, top);
        mRightBtn.setText(title);
        mRightBtn.setVisibility(View.VISIBLE);
        return mRightBtn;
    }

    public void displayRightBtn(Boolean isDisplay)
    {
        if(isDisplay)
        mRightBtn.setVisibility(View.VISIBLE);
        else
            mRightBtn.setVisibility(View.GONE);
    }


    /**
     * 设置图片按钮
     *
     * @param backgroundid
     * @return
     */
    public TextView setRightBtn(int backgroundid) {
        mRightBtn.setText("");
        mRightBtn.setVisibility(View.VISIBLE);
        mRightBtn.setPadding(0,0,0,0);
        mRightBtn.setBackgroundResource(backgroundid);

        return mRightBtn;
    }


    public TextView setRightBtn(String title, View.OnClickListener listener) {
        setRightBtn(title);
        mRightBtn.setOnClickListener(listener);
        return mRightBtn;

    }


    public void pressBar() {


    }


    @Override
    public void setContentView(int layoutResID) {
        if (layoutResID == R.layout.activity_base_tool_bar) {//如果是
            super.setContentView(layoutResID);
        } else {
            getLayoutInflater().inflate(layoutResID, mContainerFrameLayout);//否则加入到container
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:// 点击返回图标事件
                this.finish();
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public abstract void initWidget();

    public abstract void initListener();

    public void toastMsg(String t) {

    }


}
